package com.longxi.tianjintianbao.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class LeftRightView extends View {
    private Paint lPaint;//左边文字画笔
    private Paint rPaint;//右边文字画笔
    private String left = "提示";
    private String right = "内容";
    private Rect lRect = new Rect();//文字矩形
    private Rect rRect = new Rect();//文字矩形

    public LeftRightView(Context context) {
        super(context);
        initPaint();
    }

    public LeftRightView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public LeftRightView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        lPaint = new Paint();
        lPaint.setStrokeWidth(2);
        lPaint.setStyle(Paint.Style.FILL);
        lPaint.setAlpha(60);
        lPaint.setTextSize(47);
        lPaint.setAntiAlias(true);
        lPaint.setColor(Color.BLACK);

        rPaint = new Paint();
        rPaint.setStrokeWidth(3);
        rPaint.setStyle(Paint.Style.FILL);
        rPaint.setAlpha(60);
        rPaint.setTextSize(40);
        rPaint.setAntiAlias(true);
        rPaint.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        lPaint.getTextBounds(left, 0, left.length(), lRect);
        canvas.drawText(left, 50, getHeight() / 2 + lRect.height() / 2, lPaint);

        rPaint.getTextBounds(right, 0, right.length(), rRect);
        canvas.drawText(right, getWidth() - rRect.width() - 40, getHeight() / 2 + rRect.height() / 2, rPaint);
    }

    /**
     * 绘制文字
     *
     * @param canvas 画布
     */
    private void drawLeftText(Canvas canvas) {
        canvas.drawText(left, 20, getHeight() / 2, lPaint);
    }

    public void setText(String left,  String right){
        this.right = right;
        if(right == null || "".equals(right)){
            this.right = "未填写";
        }

        this.left = left;
        invalidate();
    }
}
